#!/bin/bash

PATH_LIB=$OSVC_PATH_COMP/com.replication-manager
PREFIX=OSVC_COMP_PROXY_CNF

typeset -i r=0
typeset -i i=0

OSVC_COMP_SERVERS=""
OSVC_COMP_SERVERS_LIST=""

for ip in $(echo $OSVC_COMP_GCOMM | sed "s/,/ /g")
do
	i=i+1
	buff="\\n[server$i]\\ntype=server\\naddress=$ip\\nport=3306\\nprotocol=MySQLBackend\\n"
	OSVC_COMP_SERVERS="${OSVC_COMP_SERVERS}${buff}"
	[ $i == 0 ] && {
		OSVC_COMP_SERVERS_LIST="server$i"
	} || {
		OSVC_COMP_SERVERS_LIST="${OSVC_COMP_SERVERS_LIST},server$i"
	}
done

export OSVC_COMP_SERVERS
export OSVC_COMP_SERVERS_LIST

case $1 in
check)
	for pod in $(echo /$OSVC_COMP_SERVICES_SVCNAME/pod*)
	do
		export OSVC_COMP_POD=$(basename $pod)
		typeset -i n=$(echo $OSVC_COMP_POD | sed -e "s@pod[0]*@@")

		[ $n -eq 1 ] && {
			export OSVC_COMP_STATE="MASTER"
			export OSVC_COMP_VRRP_PRIO=99
		} || {
			export OSVC_COMP_STATE="SLAVE"
			export OSVC_COMP_VRRP_PRIO=100
		}

		export OSVC_COMP_SERVER_ID=$n
		$OSVC_PYTHON $PATH_LIB/file.py ${PREFIX} check
		[ $? -eq 1 ] && r=1
	done
	exit $r
	;;
fix)
	for pod in $(echo /$OSVC_COMP_SERVICES_SVCNAME/pod*)
	do
		export OSVC_COMP_POD=$(basename $pod)
		typeset -i n=$(echo $OSVC_COMP_POD | sed -e "s@pod[0]*@@")

		[ $n -eq 1 ] && {
			export OSVC_COMP_STATE="MASTER"
			export OSVC_COMP_VRRP_PRIO=99
		} || {
			export OSVC_COMP_STATE="SLAVE"
			export OSVC_COMP_VRRP_PRIO=100
		}

		export OSVC_COMP_SERVER_ID=$n
		$OSVC_PYTHON $PATH_LIB/file.py ${PREFIX} fix
		[ $? -eq 1 ] && exit 1
	done
	exit 0
	;;
fixable)
	exit 2
	;;
esac
